﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using ETL.Business.Handlers.ProjectLogHandler;
using ETL.Business.Handlers.ProjectTaskHandler;
using ETL.Business.Handlers.ProjectTimeHandler;
using ETL.Business.Handlers.SessionHandler;
using ETL.Business.Handlers.ProjectActualResourceUsageHandler;
using ETL.Common;
using ETL.Common.Logging;

namespace ETL.Business.Helpers
{
    public class LogOn
    {
        public LogOn()
        {
            
        }

        public virtual string Execute(short? projectId, short? categoryId, short? taskId, string userName, string userId)
        {
            // Log on to project
            try
            {
                // Get current session data
                var curData = (new GetSessionDataModel()).Current();

                // Check parameter values
                if (!projectId.HasValue || !categoryId.HasValue || !taskId.HasValue)
                {
                    return string.Empty;
                }

                // Clock Off
                (new ClockOff()).Execute(userId, userName);

                // Add new project_times record
                (new AddNewProjectTime()).Execute(projectId.ToString(), categoryId.ToString(), taskId.ToString(), userName, userId);

                // Set last project, category and task
                (new SetSessionData()).Execute(Constants.SessionVars.LastProjectId, projectId.ToString());
                (new SetSessionData()).Execute(Constants.SessionVars.LastCategoryId, categoryId.ToString());
                (new SetSessionData()).Execute(Constants.SessionVars.LastTaskId, taskId.ToString());

                // Check resuming
                var resuming = (new GetSessionData()).Execute(Constants.SessionVars.Resuming);
                if(resuming.Equals("True"))
                {
                    string lastActivity = (new GenerateProjectActivity()).Execute(projectId.ToString(),
                                                                                  categoryId.ToString(),
                                                                                  taskId.ToString());
                    // set last activity to session
                    (new SetSessionData()).Execute(Constants.SessionVars.LastActivity, lastActivity);

                    // update log
                    //(new AddNewProjectLog()).
                    //    Execute(projectId, categoryId, taskId, userId, "successfully resumed previous task");

                }
                else
                {
                    // logon to new task
                    // last task doesn't exist, set current task
                    (new SetSessionData()).Execute(Constants.SessionVars.CurrentProjectId, projectId.ToString());
                    (new SetSessionData()).Execute(Constants.SessionVars.CurrentCategoryId, categoryId.ToString());
                    (new SetSessionData()).Execute(Constants.SessionVars.CurrentTaskId, taskId.ToString());

                    string lastActivity = (new GenerateProjectActivity()).Execute(projectId.ToString(),
                                                                                  categoryId.ToString(),
                                                                                  taskId.ToString());
                    // set last activity to session
                    (new SetSessionData()).Execute(Constants.SessionVars.LastActivity, lastActivity);

                    // update log
                    //(new AddNewProjectLog()).
                     //   Execute(projectId, categoryId, taskId, userId, "successfully logged onto new task");
                }
                    
                // Update start date & status of task
                (new UpdateActualResourceUsageTaskStartDate()).Execute(curData.ProjectId, curData.TaskId, DateTime.Now);

                // Show task 
                // --- DONE IN CONTROLLER ----

            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                return "Logon to task failed!";
            }
            return "Logon to task successful!";
        }
    }
}
